#!/bin/sh

# Slackware build script for Audacity

# Written by Chess Griffin <chess at chessgriffin dot com>
# Thanks to Eric Hameleers for adding the audacity.desktop
# file and other tweaks and bug fixes.  Thanks to Heinz
# Wiesinger for bug fixes and and the soundtouch and twolame
# improvements.

# Modified by the SlackBuilds.org project

PRGNAM=audacity
VERSION=1.3.6
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SOUNDTOUCH=${SOUNDTOUCH:-no}
TWOLAME=${TWOLAME:-no}

if [ "$SOUNDTOUCH" = "no" ]; then
    do_soundtouch="without"
else
    do_soundtouch="with"
fi

if [ "$TWOLAME" = "no" ]; then
    do_twolame="without"
else
    do_twolame="with"
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP || exit 1
rm -rf $PRGNAM-src-$VERSION
tar -xvf $CWD/$PRGNAM-src-$VERSION.tar.bz2 || exit 1
cd $PRGNAM-src-$VERSION || exit 1
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# The following is needed until Slackware officially ships
# autotools-1.10.
sed -i "s|am__api_version='1.10'|am__api_version='1.9'|" lib-src/portsmf/configure

CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --enable-unicode \
  --enable-nyquist \
  --enable-vamp \
  --with-expat \
  --with-libvorbis \
  --with-libmad \
  --with-libflac \
  --with-libsndfile \
  --with-libid3tag \
  --with-libresample \
  --$do_soundtouch-soundtouch \
  --$do_twolame-libtwolame \
  || exit 1

make || exit 1
make install DESTDIR=$PKG || exit 1

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# We're going to overwrite the include audacity.desktop file with ours,
# and the icon isn't added at all by upstream, so we'll do that here
install -D -m 0644 $CWD/audacity.desktop $PKG/usr/share/applications/audacity.desktop
install -D -m 0644 images/AudacityLogo48x48.xpm $PKG/usr/share/pixmaps/audacity.xpm

# Move manpage directory to proper loacation; pages are gzipped already
mv $PKG/usr/share/man $PKG/usr

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/share/doc/audacity/* $PKG/usr/doc/$PRGNAM-$VERSION
cp LICENSE.txt README.txt $PKG/usr/doc/$PRGNAM-$VERSION
rm -r $PKG/usr/share/doc
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
